<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/7
 * Time: 19:12
 */

namespace App\Game\Traits;


trait GgTrait
{
    public function ggRequest($method, $action)
    {
        $tick = time();
        $post=[];
        $post['action'] = $action['action'];
        $post['channel'] = $this->channel;
        $post['data'] = json_encode($action['data']);
        $post['tick'] = $tick;
        $post['secret'] = md5($tick.$this->token.$post['data']);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'put...'.json_encode($post).PHP_EOL, FILE_APPEND);
        $result=$this->request($method, $this->gameUrl, $post);
        return $result;
    }
    public function prepareTransfer($accountUserName, $action, $price, $transferid)
    {
        $data = [
            'action' => $action,
            'data' => [
                'acc' => $accountUserName,
                'amount' => $price,
                'serial' => $transferid,
            ],
        ];
        $output = $this->ggRequest('GET', $data);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'put...'.$output.PHP_EOL, FILE_APPEND);
        $outputarr = json_decode($output, true);
        if (!isset($outputarr['error']) || $outputarr['error'] == 0) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败'];
        }
    }

    public function transferConfirm($accountUserName, $action, $transferid)
    {
        $data = [
            'action' => $action,
            'data' => [
                'acc' => $accountUserName,
                'serial' => $transferid,
            ],
        ];
        $output = $this->ggRequest('GET', $data);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'chenck...'.$output.PHP_EOL, FILE_APPEND);
        $outputarr = json_decode($output, true);
        if (!isset($outputarr['error']) || $outputarr['error'] == 0) {
            return ['success' => true, 'data' => [], 'message' => ''];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '确认转账失败'];
        }
    }
}
